package controller

import (
	"smc-admin/models"
	"smc-admin/response"
	"smc-admin/utils"
	"strconv"

	"github.com/gin-gonic/gin"
)

type Types struct {
	Name        string `form:"name" binding:"required"`
	Thickness   string `form:"thickness" binding:"required"`   //厚度
	Place       string `form:"place" binding:"required"`       //产地
	Type        string `form:"type" binding:"required"`        //类型
	Type2       string `form:"type2" binding:"required"`       //类型2
	Brand       string `form:"brand" binding:"required"`       //品牌
	QualityTime string `form:"qualityTime" binding:"required"` //质保时间
}

type TypesById struct {
	ID int `form:"id" binding:"required"`
	Types
}

// @Tags         车膜类型
// @Summary  获取全部类型
// @Success      200       {object}  models.Types
// @Router       /types/get [get]
func GetTypes(ctx *gin.Context) {

	t := models.Types{}

	types, err := t.Find()
	if err != nil {
		panic(err.Error())
	}

	response.Success(ctx, response.Body{
		Data: types,
	})
}

// @Tags         车膜类型
// @Summary      车膜类型更新
// @Param        types  body	Types  true  "车膜类型"
// @Success      200       {object}  response.Body
// @Router       /types/update [PUT]
func UpdateTypes(ctx *gin.Context) {
	var types TypesById
	err := ctx.ShouldBindJSON(&types)
	if err != nil {
		panic(err.Error())
	}

	//更新
	i := models.Types{
		BaseModel: models.BaseModel{
			ID: types.ID,
		},
	}
	updateErr := i.Update(map[string]interface{}{
		"name":         types.Name,
		"thickness":    types.Thickness,
		"place":        types.Place,
		"type":         types.Type,
		"type2":        types.Type2,
		"brand":        types.Brand,
		"quality_time": types.QualityTime,
	})
	if updateErr != nil {
		panic(updateErr.Error())
	}
	response.Success(ctx)
}

// @Tags         车膜类型
// @Summary      车膜类型添加
// @Param        types  body	Types  true  "车膜类型"
// @Success      200       {object}  response.Body
// @Router       /types/add [POST]
func AddTypes(ctx *gin.Context) {
	var types Types
	error := ctx.ShouldBindJSON(&types)
	if error != nil {
		panic(error.Error())
	}

	i := models.Types{
		Name:        types.Name,
		Thickness:   types.Thickness,
		Place:       types.Place,
		Type:        types.Type,
		Type2:       types.Type2,
		Brand:       types.Brand,
		QualityTime: types.QualityTime,
	}
	e := i.Add()
	if e != nil {
		panic(e.Error())
	}

	response.Success(ctx)
}

// @Tags         车膜类型
// @Summary      车膜类型分页查询
// @Param        id	query	int  false  "id"
// @Param        name	query	string  false  "名称"
// @Param        pageSize	query	string  false  "分页大小"
// @Param        pageNum	query	string  false  "分页数量"
// @Success      200       {object}  response.Body
// @Router       /types/findByPage [get]
func FindTypesByPage(ctx *gin.Context) {
	name := ctx.Query("name")
	id, _ := strconv.Atoi(ctx.Query("id"))

	i := models.Types{
		BaseModel: models.BaseModel{
			ID: id,
		},
		Name: name,
	}
	data, total, err := i.FindByPage(utils.PageQuery(ctx))
	if err != nil {
		panic(err.Error())
	}
	response.Success(ctx, response.Body{
		Data: map[string]interface{}{
			"data":  data,
			"total": total,
		},
	})
}

// @Tags         车膜类型
// @Summary      车膜类型删除
// @Param        id	query	string  false  "车膜类型id"
// @Success      200       {object}  response.Body
// @Router       /types/delete [DELETE]
func DeleteTypes(ctx *gin.Context) {
	deleteId, _ := strconv.Atoi(ctx.Query("id"))

	i := models.Types{
		BaseModel: models.BaseModel{
			ID: deleteId,
		},
	}
	err := i.Delete()
	if err != nil {
		panic(err)
	}
	response.Success(ctx)
}
